<?php

class UserController extends Controller
{
	//Main layout
	public $layout = '//layouts/no_column';
	//Image mime type allowed
	private $allowed_avatar_mime_types = array(
		'image/jpeg',
		'image/gif',
		'image/png',
	);
	/**
	 * controller filter, run when call any methods from this controller
	 */
	public function filters()
    {
        return compactor();
    }
	/**
	 * Controller init
	 */
	public function init() {
		// import class paths for captcha extended
		Yii::$classMap = array_merge( Yii::$classMap, array(
			'CaptchaExtendedAction' => Yii::getPathOfAlias('common.extensions.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedAction.php',
			'CaptchaExtendedValidator' => Yii::getPathOfAlias('common.extensions.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedValidator.php'
		));
		$this->active_menu = 'mykomo';
		parent::init();
	}
	
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			'captcha'=>array(
				'class' => 'CaptchaExtendedAction', //'CCaptchaAction',
				'density' => 10,
				'lines' => 5,
				'fillSections' => 5,
				'backColor'=>0xFFFFFF,
//				'height' => 40,
//				'width' => 80,
				'minLength' => 5,
				'maxLength' => 6,
				'transparent' => true,
			),
//			'captcha'=>array(
//				'class'=>'CCaptchaAction',
//				'backColor'=>0xFFFFFF,
//			),
		);
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Find friends method: Using to find more friend from user page
	 */
	public function actionFindFriends() {
		$this->loginRequired(true);
		$this->active_menu = 'user';
		$this->layout = '//layouts/no_column';
		
		$this->pageTitle(sprintf('Find your friends'));
		
		$model = new FindFriendsForm;
		
		$data = array(
			'model' => $model,
		);
		
		if ( app()->request->isPostRequest ) {
			$model->attributes = $_POST['FindFriendsForm'];
			if ( $model->validate() ) {
				$suggested_friends = $model->findFriends(50);
				$data['suggested_friends'] = $suggested_friends;
			}
		}
		
		if ( isAjaxRequest() ) {
			$this->renderAjax('find_friends', $data);
		} else {
			$this->render('find_friends', $data);
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: user profile viewer
	 */
	public function actionProfile() {
		$this->loginRequired(true);
		
		$model = User::model()->with(array(
			'avatar',
		))->findByPk(user()->id);
		
		$this->pageTitle(sprintf('Your Page: %s', userDisplayName($model)));
		
		$this->render('view', array(
			'model' => $model,
		));
	}
	
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: User account informations
	 */
	public function actionAccount() {
		$this->loginRequired(true);
		
		$model = User::model()->with(array(
			'avatar',
		))->findByPk(user()->id);
		
		$this->pageTitle(sprintf('Your Account: %s', userDisplayName($model)));
		
		$account = new AccountForm();
		$account->loadFromUser($model);
		
		if ( app()->request->isPostRequest ) {
			$account->attributes = $_POST['AccountForm'];
			
			if ( $account->validate() ) {
				$model->attributes = $account->attributes;
				$model->birthday = strtotime(sprintf('%s/%s/%s', $account->dob_month, $account->dob_day, $account->dob_year));
				$model->usermeta = $account->usermeta;
				
				if ( $model->save(false) ) {
					$model->saveUserMeta();
					logInfo("User '$model->user_login' updated his/her account.");
					user()->addNotification('N', t('Your account updated'));
					$this->redirect($this->createUrl('/user/account'));
				} else {
					user()->addNotification('E', t('Error when Updating.'));
				}
			}
		}

		$this->render('account', array(
			'user' => $model,
			'model' => $account,
		));
	}
	
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Update user's avatar in account detail
	 */
	public function actionUploadAvatar() {
		$this->loginRequired(true);

		$user = User::model()->with(array(
			'avatar',
		))->findByPk(user()->id);

		if ( app()->request->isPostRequest ) {

			$object_type = 'user';
			$object_id = user()->id;
			$upload_base_path = Image::getUploadBasePath('user');
			$upload_path = Image::getUploadPath('user');

			$field_name = "avatar";
			$image_hndl = CUploadedFile::getInstanceByName($field_name);

			if ( $image_hndl != null ) {
				
				
				// save new image
				$image_file = $upload_path . '/' . $image_hndl->name;
				$image_path = str_replace($upload_base_path, '', $image_file);
				$image_hndl->saveAs($image_file);
				list($w, $h, $t, $a) = @getimagesize($image_file);

				$error = false;
				$mime_type = CFileHelper::getMimeType($image_file);

				// check file types: images only
				if ( !in_array($mime_type, $this->allowed_avatar_mime_types) ) {
					$error = 'Error, this image is not supported '. $mime_type;
				} elseif ( $image_hndl->size > 4 * 1024 * 1024 ) { // check file size <= 4MB
					$error = 'Error, max size is 4MB';
				}
				
				if ( empty($error) ) {
				
					$image = new Image;
					$image->isNewRecord = true;
					$image->image_id = null;
					$image->image_path = $image_path;
					$image->image_x = $w;
					$image->image_y = $h;
					$image->alt = '';
					if ( $image->save() ) {

						$imageLinkModel = new ImageLink;
						$imageLinkModel->object_type = $object_type;
						$imageLinkModel->object_id = $object_id;
						$imageLinkModel->image_id = $image->image_id;
						$imageLinkModel->type = 'A';
						if ( $imageLinkModel->save() ) {

							$this->assignVar('uploadInfo', array(
								'success' => true,
								'message' => 'Upload message succesfully',
								'file' => array(
									'image_id' => $image->image_id,
									'width' => $image->image_x,
									'height' => $image->image_y,
									'url' => img('user', $image->image_path, '', array(), 'full', true),
	//								'thumbnail' => img('usr', $image->image_path, '', array(), array('width' => 140, 'height' => 140), true),
								),
							));

						} else {

							$this->assignVar('uploadInfo', array(
								'success' => false,
								'message' => 'Error when upload your picture.',
							));

						}

					} else {

						$this->assignVar('uploadInfo', array(
							'success' => false,
							'message' => 'Error when upload your picture.',
						));

					}
				} else {
					@unlink($image_file);
					$this->assignVar('uploadInfo', array(
						'success' => false,
						'message' => $error,
					));
				}

				$this->ajaxResponse('');
				app()->end();

			}

			if ( getPost('choose_avatar') ) {
				$image_id = (int) getPost('image_id');
				$x = (int) getPost('x');
				$y = (int) getPost('y');
				$x2 = (int) getPost('x2');
				$y2 = (int) getPost('y2');
				$w = (int) getPost('h');
				$h = (int) getPost('w');

				$image = Image::model()->findByPk($image_id);

				if ( !empty($image) ) {

					$cropped = img_crop($object_type, $image->image_path, $x, $y, $w, $h);

					$img = new Image;
					$img->isNewRecord = true;
					$img->image_id = null;
					$img->image_path = $cropped;
					$img->image_x = $w;
					$img->image_y = $h;
					$img->alt = '';

					if ( $img->save() ) {

						$imageLinkModel = ImageLink::model()->findByAttributes(array(
							'object_id' => $object_id,
							'object_type' => $object_type,
							'type' => 'M',
						));

						if ( empty($imageLinkModel) ) $imageLinkModel = new ImageLink;

						$imageLinkModel->object_type = $object_type;
						$imageLinkModel->object_id = $object_id;
						$imageLinkModel->image_id = $img->image_id;
						$imageLinkModel->type = 'M';
						if ( $imageLinkModel->save() ) {

							user()->addNotification('N', 'Your picture updated.');
							$this->assignVar('force_reload', true);

						} else {

							user()->addNotification('E', 'Error when upload your picture.');

						}

					} else {

						user()->addNotification('E', 'Error when upload your picture.');

					}

				}
				
			}
			
		}
		
		
		$this->renderAjax('upload_avatar', array(
			'user' => $user,
		));
	} 
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Show current user informations
	 */
	public function actionView() {
		$name = getParam('name');
		
		$model = User::model()->findByAttributes(array(
			'seo_name' => $name,
			'status' => 'A',
//			'user_type' => 'C',
		));
		
		if ( empty($model) ) {
			throw new CHttpException(404, t('Profile not found'));
		}
		
		if ( $model->user_id == user()->id ) $this->redirect($this->createUrl('/user/profile'));
		
		$this->pageTitle(sprintf('Your page: %s', userDisplayName($model)));
		
		$this->render('view', array(
			'model' => $model,
		));
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Show current user notifications list
	 */
	public function actionNotifications() {
		$this->loginRequired(true);
		
		$friend_requests = FriendRequest::model()->findAllByAttributes(array(
			'to_user_id' => user()->id,
		));

		$user_messages = UserMessage::model()->findAllByAttributes(array(
			'status' => 'A',
			'to_user_id' => user()->id,
		));
		
		$this->render('notifications', array(
			'friend_requests' => $friend_requests,
			'user_messages' => $user_messages,
		));
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Update message info when user show the message
	 */
	public function actionActionMessage() {
		$this->loginRequired(true);

		$message_id = getQuery('message_id');
		$act = getQuery('act');
		$message = UserMessage::model()->findByPk($message_id);

		if ( $message->to_user_id == user()->id ) { // check user => OK
			// update message status
			$message->status = 'H';
			$message->save();

			if ( $act == 'accept' ) {
				$this->redirect($message->action_url . '&return_url='.  urlencode($this->createUrl('/user/notifications')));
			} else { // reject
				$this->redirect($this->createUrl('/user/notifications'));
			}
		}

		// @TODO: show error message or something else...
		$this->redirect($this->createUrl('/'));
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Update Friend request status
	 */
	public function actionAcceptFriend() {
		$this->loginRequired();
		
		$name = getParam('name');

		$model = User::model()->findByAttributes(array(
			'seo_name' => $name,
			'status' => 'A',
		));

		if ( FriendRequest::sentRequest($model->user_id, user()->id) ) {
			Friend::makeFriend(user()->id, $model->user_id);
			
			// FriendRequest::removeRequest($model->user_id, user()->id);
		}
		
		if (isAjaxRequest() ) {
			$this->assignVar('force_redirection', $this->createUrl('/user/notifications'));
			$this->ajaxResponse('');
			app()->end();
		} else {
			$this->redirect($this->createUrl('/user/notifications'));
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Update Friend Request Message
	 */
	public function actionRejectFriend() {
		$this->loginRequired(true);

		$name = getParam('name');

		$model = User::model()->findByAttributes(array(
			'seo_name' => $name,
			'status' => 'A',
		));
		
		if ( FriendRequest::sentRequest($model->user_id, user()->id) ) {
			FriendRequest::removeRequest($model->user_id, user()->id);
		}
		
		if (isAjaxRequest() ) {
			$this->assignVar('force_redirection', $this->createUrl('/user/notifications'));
			$this->ajaxResponse('');
			app()->end();
		} else {
			$this->redirect($this->createUrl('/user/notifications'));
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Send Friend Request to user.
	 */
	public function actionFriend() {
		$this->loginRequired(true);
		
		$name = getParam('name');
		
		$model = User::model()->findByAttributes(array(
			'seo_name' => $name,
			'status' => 'A',
		));
		
		if ( empty($model) ) {
			throw new CHttpException(404, t('Profile not found.'));
		}
		
		$result = FriendRequest::sendRequest(user()->id, $model->user_id);
		if ( $result ) {
			user()->addNotification('N', t('Friend invitaion already sent.'));
		} else {
			user()->addNotification('E', t('Can not invite he/she now.'));
		}
		
		if ( isAjaxRequest() ) {
			$this->assignVar('friend', 'ok');
			$this->assignVar('status', t('Friend invitaion already sent.'));
			$this->renderAjax('view', array(
				'model' => $model,
			));
			app()->end();
		} else {
			$this->redirect($model->profileUrl);
		}

	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Login method
	 */
	public function actionSignin() {
		if ( !Yii::app()->user->isGuest ) $this->redirect(array('/site/index'));

		$model = new LoginForm;

		if ( app()->request->isPostRequest ) {

			$formdata = array(
				'username' => $_POST['user_login'],
				'password' => $_POST['password'],
			);

			$model->attributes = $formdata;
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()) {
				Yii::app()->user->returnUrl = getParam('returnUrl') ? getParam('returnUrl') : '/';

				// trigger event
				Activity::eventTrigger('login', user()->id, 'user');
				
				if (isAjaxRequest()) {
					$this->assignVar('force_redirection', Yii::app()->user->returnUrl);
				} else {
					$this->redirect(Yii::app()->user->returnUrl);
				}
			}
		}

		// display the login form
		if ( isAjaxRequest() ) {
			$this->renderAjax('login', array('model'=>$model));
		} else {
			$this->render('login',array('model'=>$model));
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Register new account
	 */
	public function actionRegister() {
		if ( !user()->isGuest ) $this->redirect(array('/site/index'));

		$model = new RegisterForm;
		$this->active_menu = 'register';
		if ( app()->request->isPostRequest ) {

			if(isset($_POST['RegisterForm']))
			{
				$model->attributes = $_POST['RegisterForm'];

				// validate user input and redirect to the previous page if valid
				if($model->validate()) {
					// create User
					$userdata = array(
						'user_login' => $model->username,
						'email' => $model->username,
						'password' => $model->password1,
						'name' => $model->name,
						'status' => 'A',
						'user_type' => 'C',
					);
					$user = User::model();
					if ( true === ($error = $user->createUser($userdata)) ) {
						// trigger event
						//Activity::eventTrigger('register', $user->user_id, 'user', $user->user_id);
						
						// add some books for user: // y thien do long ky, noi trai tim hoi ngo, Sao biển và nhện, Quà tặng cho người muốn thành công, Cuộc Đời Và Những Cuộc Phiêu Lưu Của Santa Claus
						//$user->giftFreeBooks();
						logInfo("New user '$user->user_login' is registered.");
						// do login
						$login_form = new LoginForm;
						$login_form->username = $model->username;
						$login_form->password = $model->password1;
						$login_form->rememberMe = true;

						if($login_form->validate() && $login_form->login()) {
							if (isAjaxRequest()) {
								$this->assignVar('force_redirection', Yii::app()->user->returnUrl);
							} else {
								$this->redirect(Yii::app()->user->returnUrl);
							}
						} else {
							print_r($login_form->getErrors());
						}
					} else {
						if ( $error['code'] == '11' ) {
							$model->addError('username', t('Username already exists'));
						} elseif ( $error['code'] == '13' ) {
							$model->addError('username', t('Unknown error'));
						} else {
							print_r($error);
						}
					}
				}
			}
		}

		// display the login form
		if ( isAjaxRequest() ) {
			$this->renderAjax('register', array('model'=>$model));
		} else {
			$this->render('register',array('model'=>$model));
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Lost password recovery page
	 */
	public function actionforgotPassword()
	{
		$this->breadcrumbs = array(
			t('Forgot Password') => array('/user/forgotPassword'),
		);

		$model = new ForgotPasswordForm;

		if ( app()->request->isPostRequest ) {
			$model->attributes = $_POST['ForgotPasswordForm'];
			// validate user input and redirect to the previous page if valid
			if( $model->validate() ) {
				$user = User::model()->findByAttributes(array('user_login' => $model->username));

				$newPassword = User::generatePassword();
				$user->password = $user->password1 = $user->password2 = $user->hashPassword($newPassword);

				if ( $user->save() ) {
					$mail = new YiiMailer();
					$mail->setView('forgot_password');
					$mail->setData(array(
						'user' => $user,
						'new_password' => $newPassword,
						'login_url' => $this->createAbsoluteUrl('/user/login'),
					));
					//render HTML mail, layout is set from config file or with $mail->setLayout('layoutName')
					$mail->render();

					//set properties as usually with PHPMailer
					$mail->From = 'sender@email.com';
					$mail->FromName = 'Sender';
					$mail->Subject = 'Your new password';

					$mail->AddAddress($user->email);

					//send
					if ( $mail->Send() ) {
						$mail->ClearAddresses();

						$this->redirect(array('/user/login', 'message' => 3));
					}

					
				}
			}
		}

		$this->render('forgot_password', array(
			'model' => $model,
		));
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Show the openID Provider to choose a login methods.
	 */
	public function actionOpenId() {

		Yii::import('common.extensions.openid.LightOpenID');
		$openid = new LightOpenID(param('frontUrl'));

		$service = getParam('service');
		$referer = getParam('referer');
		$discovery_urls = array(
			'google' => 'https://www.google.com/accounts/o8/id',
			'yahoo' => 'http://me.yahoo.com/',
		);

		if ( !array_key_exists($service, $discovery_urls) ) // currently we just support Google and Yahoo
			$this->redirect(array('/site/index'));

		$openid->identity = $discovery_urls[$service];

		$current_url = $referer;

		//$openid->returnUrl = param('frontUrl') . 'index.php?r=user/openIdReturn&openid_return_url='.urlencode($referer);
		$openid->returnUrl = param('frontUrl') . $this->createUrl('/user/openIdReturn', array('openid_return_url' => $referer)); //. '&openid_return_url='.urlencode($referer);
		$openid->required = array('namePerson/first', 'namePerson/last', 'contact/email');

		$this->redirect($openid->authUrl());

	}
	/**
	 * @Author: nguyen Tuan Kiet
	 * @Method: Create user's password.
	 */
	private function createRandomPassword($length = 8) {
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";
		srand((double)microtime()*1000000);
		$i = 0;
		$pass = '' ;

		while ($i < $length) {
			$num = rand() % 33;
			$tmp = substr($chars, $num, 1);
			$pass = $pass . $tmp;
			$i++;
		}

		return $pass;
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Request login by OpenID
	 */
	public function actionOpenIdReturn() {
		$redirect_url = param('frontUrl');
		if ( !empty($_REQUEST['openid_return_url']) )
			$redirect_url = $_REQUEST['openid_return_url'];

		Yii::import('common.extensions.openid.LightOpenID');
		$openid = new LightOpenID(param('frontUrl'));

		if ( $openid->validate() ) {
			$identity = $openid->identity;
			$attributes = $openid->getAttributes();
			$email = @$attributes['contact/email'];
			$first_name = @$attributes['namePerson/first'];
			$last_name = @$attributes['namePerson/last'];
			$password = md5($email);
			$user_login = $email;

			$user = User::model()->find('LOWER(user_login)=?', array($user_login));
			if ( $user === null ) {
				$result = idApi('getUserInfo', array('user_login' => $user_login));
				if ( $result['success'] ) {
					$userdata = $result['user_data'];

					$user = User::model();
					$user->attributes = $userdata;
					$user->user_id = $userdata['user_id'];
					$user->isNewRecord = true;

					$user->save(false);
				}
			}

			$user = User::model()->find('LOWER(user_login)=?', array($user_login));
			if ( $user === null ) {
				// create User
				$userdata = array(
					'user_login' => $user_login,
					'email' => $email,
					'password' => $password,
					'name' => ( (!empty($first_name) || !empty($last_name)) ? $first_name . ' ' . $last_name : substr_replace($email, '', strpos($email, '@')) ),
					'status' => 'A',
					'user_type' => 'C',
				);

				if ( !($error = User::model()->createUser($userdata)) ) {
					$this->render('/site/error');
				}
			}

			$result = idApi('getUserInfo', array('user_login' => $user_login));
			$userdata = $result['user_data'];

			// do login
			$_identity = new UserIdentity($user_login, $password);
			$_identity->setId($userdata['user_id']);
			$_identity->username = $userdata['user_login'];
			$_identity->setState('last_login', time());
			$_identity->errorCode = UserIdentity::ERROR_NONE;

			$duration = 3600*24; // 30 days or 1 day
			Yii::app()->user->login($_identity,$duration);

			//$this->redirect($redirect_url);
			echo '<html><head></head><body>';
			echo 'Waiting for redirecting...';
			echo '<script>window.opener.location = "'.$redirect_url.'"; window.close();</script>';
			echo '</body></html>';
			app()->end();
		}
	}
   	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Login
	 */
	public function actionLogin()
	{
		if ( !Yii::app()->user->isGuest ) {
			if (isAjaxRequest()) {
				$this->assignVar('force_redirection', $this->createUrl('/site/index'));
			} else {
				$this->redirect($this->createUrl('/site/index'));
			}
		}

		$service = getParam('service');
		if ( !empty($service) ) {
			//$this->redirect(array('/user/openId', 'service' => $service, 'referer' => $_SERVER['HTTP_REFERER']));

			$this->redirect(array('/user/auth', 'service' => $service, 'return_url' => $_SERVER['HTTP_REFERER']));
		}

		$model = new LoginForm;

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes = $_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			
			if($model->validate() && $model->login()) {
				Yii::app()->user->returnUrl = getParam('returnUrl') ? getParam('returnUrl') : $this->createUrl('/site/index');

				// trigger event
				//Activity::eventTrigger('login', user()->id, 'user');
				
				if (isAjaxRequest()) {
					$this->assignVar('force_redirection', Yii::app()->user->returnUrl);
				} else {
					$this->redirect(Yii::app()->user->returnUrl);
				}
			}
		}

		// display the login form
		if ( isAjaxRequest() ) {
			$this->renderAjax('login', array('model'=>$model));
		} else {
			$this->render('login',array('model'=>$model));
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Login without redirect to another page
	 */
	public function actionSilentLogin()
	{
		if ( !Yii::app()->user->isGuest ) $this->redirect($this->createUrl('/site/index'));

		$model = new LoginForm;

		$login_info = array(
			'username' => urldecode(getParam('email')),
			'password' => getParam('password'),
		);

		$model->attributes = $login_info;
		// validate user input and redirect to the previous page if valid
		if($model->validate() && $model->login()) {
			Yii::app()->user->returnUrl = getParam('returnUrl') ? getParam('returnUrl') : $this->createUrl('/site/index');

			// trigger event
			Activity::eventTrigger('login', user()->id, 'user');
			
			if (isAjaxRequest()) {
				$this->assignVar('force_redirection', Yii::app()->user->returnUrl);
			} else {
				$this->redirect(Yii::app()->user->returnUrl);
			}
		}
	}
	/**
	 * @Author: Nguyen Tuan Kiet
	 * @Method: Logout from the page.
	 */
	public function actionLogout() {
		$user_id = user()->id;
		
		//Yii::app()->user->returnUrl = Yii::app()->request->getUrlReferrer();

		// logout socials
		//require_once(Yii::getPathOfAlias('hybridauth').'/Hybrid/Auth.php');
		//$config = User::getAuthConfig();
		//$hybridauth = new Hybrid_Auth( $config );
		//$hybridauth->logoutAllProviders();

		// trigger event
		//Activity::eventTrigger('logout', $user_id, 'user', $user_id);
		
		// logout app
		Yii::app()->user->logout();
		// redirect
		$this->redirect($this->createUrl('/site/index'));
	}
 	/**
	 * @Author: Nguyen TUan Kiet
	 * @MEthod: Show the administrator page
	 */
	public function actionAdminpage()
	{
		$this->loginRequired(true);
		$user_id = user()->id;
		$user = User::model()->findByPk($user_id);
		if($user->user_type == "A")
		{
			$this->redirect('admin/index.php?r=site/login&from='.$user->user_id);
		}
		else
		{
			$this->render('adminpage');
		}
	}
}